//
//  NewFeatureViewController.swift
//  SinaWeiBo20
//
//  Created by tarena on 16/3/23.
//  Copyright © 2016年 tarena. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

//定义图片的数量
private let imageCount = 4
class NewFeatureViewController: UICollectionViewController {
    
//    private var pageControl : UIPageControl
    private lazy var pageControl : UIPageControl = {
       let pageContro = UIPageControl(frame: CGRectMake((screenW - 100)*0.5,screenH - 100,100,35))
        pageContro.numberOfPages = imageCount
        pageContro.pageIndicatorTintColor = UIColor.grayColor()
        pageContro.currentPageIndicatorTintColor = UIColor.orangeColor()
        return pageContro
    }()
    
    init(){
        //实例化layout
        let layout = UICollectionViewFlowLayout()
        //1.指定layout 的itemSize cell在实例化的时候 就会使用这个size
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        //页面滚动方式
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        //设置
       view .addSubview(pageControl)
        
        
        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        cell.index = indexPath.item
        return cell
    }
    
    
   
    //MARK: 停止滚动的协议方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / screenW)
        
        if page == imageCount - 1 {
            //设置 开始按钮 的hidden
            
            //开始动画效果
            if let cell = collectionView?.visibleCells().last as? NewFeatureCell {
                cell.startAnimation()
            }
        }
        
    }
    //滚动时候
    override func scrollViewDidScroll(scrollView: UIScrollView) {
       pageControl.currentPage = Int(round(scrollView.contentOffset.x / view.bounds.width))
    }


}





/*******************************************************************

   ＊＊＊＊ NewFeatureCell  单元格

*******************************************************************/

class NewFeatureCell : UICollectionViewCell {
    
    //1. 实例化子视图控件
    private lazy var iconImage: UIImageView = UIImageView()
    
    //开始按钮
    private lazy var startButton: UIButton = UIButton(title: "开始体检", backImage: "new_feature_finish_button", color: nil)
    
    var index: Int = 0 {
        didSet {
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            startButton.hidden = true
        }
    }
    
    //MARK: 按钮的点击事件
    @objc private func start(){
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewController, object: nil)
    }
    
    //开始动画的效果
    private func startAnimation() {
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startButton.hidden = false
            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            }) { (_) -> Void in
                print("OK")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        print(frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //2. 设置UI
    private func setupUI() {
        //1.添加子视图
        contentView.addSubview(iconImage)
        contentView.addSubview(startButton)
        
        //2.设置约束
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)
        }
        
        //3.设置点击事件
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
        
    }
    
    
    
    
    
}





